package com.czl.module_storehouse.adapter;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.data.bean.tengyun.SortBeanType;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_storehouse.R;
import com.lihang.ShadowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class StockListChildAdapter extends BaseMultiItemQuickAdapter<SortBean, BaseViewHolder> {

    private final List<SortBean> mSortBeanList = new ArrayList<>();

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public StockListChildAdapter(List<SortBean> data) {
        super(data);
        addItemType(SortBeanType.ItemViewType.VIEW_TYPE_HEADER, R.layout.item_stock_list_header);
        addItemType(SortBeanType.ItemViewType.VIEW_TYPE_SUB_HEADER, R.layout.item_stock_list_header);
        addItemType(SortBeanType.ItemViewType.VIEW_TYPE_ITEM, R.layout.item_stock_list_content);
    }

    public void addAll(List<LocatListBean> list, String tag) {
        if (TextUtils.isEmpty(tag)) {
            return;
        }
        if (list == null) {
            return;
        }
        List<SortBean> sortBeanList = new ArrayList<>();
        for (LocatListBean locatListBean : list) {
            if (tag.equals(String.valueOf(locatListBean.getStorehouseLocatId()))) {
                SortBean sortHeader = new SortBean();
                sortHeader.setStorehouseLocatName(locatListBean.getStorehouseLocatName());
                sortHeader.setItemViewType(SortBeanType.ItemViewType.VIEW_TYPE_HEADER);
                sortBeanList.add(sortHeader);
                List<SortBean> sortList = locatListBean.getSortList();
                if (sortList == null) {
                    continue;
                }
                for (SortBean sortBean : sortList) {
                    sortBean.setItemViewType(SortBeanType.ItemViewType.VIEW_TYPE_ITEM);
                    sortBeanList.add(sortBean);
                }
                break;
            }
        }
        addData(sortBeanList);
    }

    public void addAll(List<LocatListBean> list) {
        mSortBeanList.clear();
        addSortData(list, 0);
        addData(mSortBeanList);
    }

    private void addSortData(List<LocatListBean> list, int level) {
        if (list == null || list.isEmpty()) {
            return;
        }
        for (LocatListBean bean : list) {
            SortBean sortHeader = new SortBean();
            if (level == 0) {
                sortHeader.setItemViewType(SortBeanType.ItemViewType.VIEW_TYPE_SUB_HEADER);
            } else {
                sortHeader.setItemViewType(SortBeanType.ItemViewType.VIEW_TYPE_HEADER);
            }
            sortHeader.setStorehouseLocatName(bean.getStorehouseLocatName());
            mSortBeanList.add(sortHeader);
            List<SortBean> sortList = bean.getSortList();
            if (sortList != null) {
                for (SortBean sortBean : sortList) {
                    sortBean.setItemViewType(SortBeanType.ItemViewType.VIEW_TYPE_ITEM);
                    mSortBeanList.add(sortBean);
                }
            }
            addSortData(bean.getChildList(), level + 1);
        }
    }

    public void addSortAll(List<SortBean> list, String name) {
        if (list == null || list.isEmpty()) {
            return;
        }
        List<SortBean> sortBeanList = new ArrayList<>();
        SortBean sortHeader = new SortBean();
        sortHeader.setStorehouseLocatName(name);
        sortHeader.setItemViewType(SortBeanType.ItemViewType.VIEW_TYPE_HEADER);
        sortBeanList.add(sortHeader);
        for (SortBean sortBean : list) {
            sortBean.setItemViewType(SortBeanType.ItemViewType.VIEW_TYPE_ITEM);
            sortBeanList.add(sortBean);
        }
        addData(sortBeanList);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {

        int itemViewType = helper.getItemViewType();
        switch (itemViewType) {
            case SortBeanType.ItemViewType.VIEW_TYPE_HEADER:
                helper.setText(R.id.tv_header_name, item.getStorehouseLocatName());
                break;
            case SortBeanType.ItemViewType.VIEW_TYPE_SUB_HEADER:
                TextView textView = helper.getView(R.id.tv_header_name);
                textView.setText(item.getStorehouseLocatName());
                LinearLayout linearLayout = helper.getView(R.id.layout_root);
                linearLayout.setGravity(Gravity.CENTER);
                break;
            case SortBeanType.ItemViewType.VIEW_TYPE_ITEM:
                helper.setText(R.id.tv_name, item.getSortName())
                        .setText(R.id.tv_num, String.valueOf(item.stockNum()))
                        .setText(R.id.tv_model, getContext().getString(R.string.code_risk, item.getSortCode()))
                        .setText(R.id.tv_standard, getContext().getString(R.string.standard_risk, item.getSortModel()));
                ImageView imageView = helper.getView(R.id.iv_name);
                GlideUtils.loadImage(getContext(), imageView, item.getSortImg(), R.mipmap.pic_default_loading, R.mipmap.pic_default_error);
                ShadowLayout shadowLayout = helper.getView(R.id.shadowLayout);
                shadowLayout.setShadowHiddenBottom(true);
                helper.getView(R.id.cl_content).setOnClickListener((View.OnClickListener) v -> {
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(StockListChildAdapter.this,
                                v, helper.getLayoutPosition());
                    }
                });
                break;
            default:
                break;
        }
    }
}

